import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2023-06-07
 * Time: 20:23
 */
public class JDBCInset {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);

        //JDBC 需要以下步骤来完成开发.

        //1.创建并初始化一个数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java107?characterEncoding=utf8&useSSL=false");
        //setURl()这个方法子类有,父类没有,想使用就得把父类引用转回子类引用.
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.和数据库服务器建立连接
        Connection connection = dataSource.getConnection();
        //3.从控制塔读取用户的输入内容
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学号:");
        int id = scanner.nextInt();
        //4.构造 SQL 语句
        String sql = "insert into student value(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        //这个打印需要加到拼接数据之后
        System.out.println(statement);
        //5.执行 SQL 语句
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);
        //6. 释放必要的资源
        statement.close();
        connection.close();
    }
}
